﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using CID.Domain.Infrastructure;
using CID.Domain.Users;
using CID.Web.Models.Infrastructure;
using CID.Web.Support;
using CID.Web.Support.TemplateContentEdition;
using NailsFramework.IoC;
using NailsFramework.Persistence;
using CID.Web.Support.Ajax;
using CID.Support.Exceptions;

namespace CID.Web.Controllers
{
    [CIDAuthorize(UserRole.Architect, UserRole.Manager, UserRole.Operator, UserRole.Developer)]
    public class TemplateController : AjaxEnabledBaseCRUDController<ITemplate, Template, TemplateViewModel>
    {
        [Inject]
        public IBag<TemplateType> TemplateTypes { private get; set; }
        private IEnumerable<TemplateType> EnabledTemplateTypes { get { return TemplateTypes.Where(x => x.Enabled); } }

        protected override Template CreateModel(TemplateViewModel viewModel)
        {
            var templateType = TemplateTypes.GetById(viewModel.TemplateTypeId.Value);
            ITemplate result = null;
            switch (viewModel.Purpose)
            {
                case TemplatePurpose.Activation:
                    result = templateType.CreateActivationTemplate(viewModel.Name, viewModel.Description, new byte[0]);
                    break;
                case TemplatePurpose.Deactivation:
                    result = templateType.CreateDeactivationTemplate(viewModel.Name, viewModel.Description, new byte[0]);
                    break;
                case TemplatePurpose.Deployment:
                    result = templateType.CreateDeploymentTemplate(viewModel.Name, viewModel.Description, new byte[0]);
                    break;
            }
            return (result as Template);
        }

        protected override void PrepareViewModelForView(TemplateViewModel viewModel)
        {
            base.PrepareViewModelForView(viewModel);

            viewModel.SetAvailableTemplateTypes(EnabledTemplateTypes);

            if (viewModel.TemplateTypeId.HasValue)
            {
                var templateType = TemplateTypes.GetById(viewModel.TemplateTypeId.Value);
                if (String.IsNullOrEmpty(viewModel.TemplateType))
                    viewModel.TemplateType = templateType.Name;

                viewModel.ContentEditor = TemplateContentEditorRegistry.Instance.GetEditor(templateType);
            }
        }

        protected virtual void PrepareViewModelForView(TemplateViewModel viewModel, int originalId)
        {
            PrepareViewModelForView(viewModel);
            viewModel.PostUrl = Url.Action("Create");
        }

        public virtual ViewResult CreateCopy(int originalId)
        {
            var template = GetModel(originalId);
            var viewModel = new TemplateViewModel(template.CreateCopy());
            PrepareViewModelForView(viewModel, originalId);
            CurrentUnitOfWork.Cancel();
            return View("Create", viewModel);
        }
    }
}
